package com.isunland.managesystem.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;

/**
 * 对象功能: 作业轨迹界面
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/12 19:50
 */

public class LocaleWorkQueryCountPathFragment extends BaseFragment {

  @BindView(R.id.wv_map) WebView mWv;
  private String mUrl;

  /** 进度 */
  //private MyProgressDialog mDialog;
  @SuppressLint("SetJavaScriptEnabled") @Override public View onCreateView(LayoutInflater inflater,
      @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_task_path, container, false);
    ButterKnife.bind(this, v);
    WebSettings settings = mWv.getSettings();
    //设置可调用js.
    settings.setJavaScriptEnabled(true);
    mWv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    //final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
    mWv.setWebViewClient(new WebViewClient() {
      //在当前页面打开链接.
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }

      @Override public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        //showDialogLoading(getString(R.string.loading));
      }

      public void onPageFinished(WebView view, String url) {
        Log.i("", "Finished loading URL: " + url);
        //if (mDialog != null && mDialog.isVisible()) {
        //
        //  mDialog.dismiss();
        //}
      }

      @SuppressWarnings("deprecation")
      public void onReceivedError(WebView view, int errorCode, String description,
          String failingUrl) {
        Log.e("", "Error: " + description);
        mWv.loadUrl("file:///android_asset/error.html");
        //alertDialog.setTitle("错误");
        //alertDialog.setMessage("请刷新重试");
        //alertDialog.show();
      }
    });
    return v;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_refresh, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_refresh:
        loadUrl(mUrl);
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    mUrl = mBaseParams.getRemark();
    setTitleCustom(mBaseParams.getTitle());
    MyUtils.syncCookie(mActivity);
    loadUrl(mUrl);
  }

  private void loadUrl(String mUrl) {
    //if (TextUtils.isEmpty(mUrl)) {
    //    ToastUtil.show("无法获取作业设备信息");
    //    return;
    //}
    mActivity.setResult(Activity.RESULT_OK);
    mUrl = mUrl.replaceAll(" ", "%20");
    LogUtil.i("url=" + mUrl);
    mWv.loadUrl(mUrl);
  }

  //private void showDialogLoading(String msg) {
  //  mDialog = MyProgressDialog.newInstence(msg);
  //  FragmentManager fm = getActivity().getSupportFragmentManager();
  //  mDialog.setTargetFragment(this, 0);
  //  mDialog.show(fm, "");
  //}
}
